<!--
Copyright 2017 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
-->

<link rel="import" href="../../modules/utils/utils.html">

<link rel="import" href="../../bower_components/polymer-decorators/polymer-decorators.html">
<link rel="import" href="../../bower_components/iron-icons/editor-icons.html">
<link rel="import" href="../../bower_components/iron-icons/hardware-icons.html">
<link rel="import" href="../../bower_components/iron-icons/iron-icons.html">
<link rel="import" href="../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../bower_components/paper-checkbox/paper-checkbox.html">
<link rel="import" href="../../bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="../../bower_components/paper-item/paper-item.html">
<link rel="import" href="../../bower_components/polymer-decorators/polymer-decorators.html">

<dom-module id="item-list">
  <template>

    <style include="datalab-shared-styles">
      :host {
        display: flex;
        flex-direction: column;
      }
      div {
        font-family: var(--app-content-font-family);
        font-size: var(--app-content-font-size);
        color: var(--primary-fg-color);
      }
      #headerContainer {
        flex: 0 0 var(--toolbar-height);
        z-index: 1;
      }
      #header {
        user-select: none;
        display: flex;
        line-height: calc(var(--toolbar-height) - 1px);
        border-bottom: 1px solid var(--border-color);
      }
      #listContainer {
        flex: 1 1 auto;
        overflow: auto;
      }
      .row {
        user-select: none;
        padding: 0px;
        display: flex;
        --paper-item-min-height: 35px;
        line-height: 35px;
        cursor: default;
      }
      .row:nth-of-type(even) {
        background-color: var(--secondary-bg-color);
      }
      .row:hover {
        background-color: var(--selection-bg-color);
      }
      .row[selected] > * {
        color: var(--selection-fg-color);
      }
      .row-details {
        padding: 0px;
      }
      .checkbox-col {
        flex: 0 0 65px;
        padding-left: 15px;
      }
      .checkbox-col > iron-icon {
        width: 17px;
        height: 17px;
      }
      .checkbox-col > paper-checkbox {
        --paper-checkbox-size: 15px;
        --paper-checkbox-unchecked-color: var(--neutral-fg-color);
        --paper-checkbox-checked-color: var(--selection-fg-color);
      }
      .first-col {
        flex-grow: 1;
      }
      .second-col {
        flex-basis: 100px;
      }
      .title {
        padding-left: 5px;
        line-height: 26px;
      }
      .column {
        flex: 1.5;
      }
      #header > .column {
        color: var(--primary-fg-color);
        font-size: 13px;
      }
      .column-button {
        display: flex;
        justify-content: flex-start;
        width: 100%;
        padding: 0 10px;
        margin: 0;
        border-radius: 0;
        text-transform: none;
      }
      .column-button > iron-icon {
        width: 15px;
        padding-left: 3px;
      }
      .column + .column {
        flex: 1;
        max-width: 150px;
        color: var(--neutral-fg-color);
        padding-left: 10px;
      }
      #filterToggle {
        --paper-icon-button: {
          width: 34px;
          height: 34px;
        };
      }
      .filterShown--true {
        color: var(--selection-fg-color);
      }
      .filterShown--false {
        color: var(--disabled-fg-color);
      }
      #filter-div {
        position: relative;
      }
      #filter-icon {
        position: absolute;
        top: 8px;
        right: 10px;
        color: var(--disabled-fg-color);
        width: 18px;
        height: 18px;
      }
      #filterBox {
        height: 35px;
        width: 100%;
        border: solid 1px var(--border-color);
        text-indent: 75px;
        padding: 5px;
        box-sizing: border-box;
        background-color: var(--primary-bg-color);
        color: var(--primary-fg-color);
        outline: none;
      }
      #filterBox:hover {
        border: solid 1px var(--outline-color);
      }
      #filterBox:active {
        border: solid 1px var(--outline-color);
      }
    </style>

    <!--Header row-->
    <!--Need this extra div because #header has a display modifier, cannot set hidden-->
    <div id="headerContainer" hidden$="{{hideHeader}}">
      <div id="header">
        <div class="checkbox-col">
          <paper-checkbox id="selectAllCheckbox" on-checked-changed="_selectAllChanged"
                          checked={{_isAllSelected}} hidden$="{{_hideCheckboxes}}">
          </paper-checkbox>
          <paper-icon-button id="filterToggle" class$="filterShown--[[_showFilterBox]]"
                             icon="filter-list" on-click="_toggleFilter"
                             title="Filter by [[columns.0.name]]"></paper-icon-button>
        </div>
        <template is="dom-repeat" items="{{columns}}" as="column">
          <div class="column">
            <paper-button class="column-button" on-click="_columnButtonClicked">
              {{column.name}}
              <iron-icon class="sort-icon"></iron-icon>
            </paper-button>
          </div>
        </template>
      </div>
      <div id="filter-div" hidden$="[[!_showFilterBox]]">
        <iron-icon icon="filter-list" id="filter-icon"></iron-icon>
        <input id="filterBox" placeholder="Filter by [[columns.0.name]]"
               value="{{_filterString::input}}"></input>
      </div>
    </div>

    <!--Item list-->
    <div id="listContainer">
      <template is="dom-repeat" id="list" items="{{rows}}" as="row"
                filter="{{_computeFilter(_filterString)}}" initial-count="100">
        <paper-item class="row" selected$={{row.selected}} on-click="_rowClicked"
                    on-dblclick="_rowDoubleClicked">
          <div class="checkbox-col">
            <paper-checkbox checked={{row.selected}} hidden$="{{_hideCheckboxes}}">
            </paper-checkbox>
            <iron-icon hidden$="{{!row._icon}}" icon="{{row.icon}}" src="{{row.src}}"></iron-icon>
          </div>
          <template is="dom-repeat" items="{{row.columns}}" as="column" index-as="columnIndex">
            <div class="column">
              <span>[[_formatColumnValue(column, columnIndex, columns)]]</span>
            </div>
          </template>
        </paper-item>
        <div id="details" class="row-details" hidden$="{{!row.showInlineDetails}}"></div>
      </template>
    </div>

  </template>
</dom-module>

<script src="item-list.js"></script>
